package icu.lingjie.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Slf4j
public class ConfigUtil {

    /**
     * 世界的长宽高都是 2 的 n 次方，LOG2_X表示世界宽度X为 2 ** LOG2_X
     */
    public static int LOG2_X;
    @Value("${lingjie.X.log2}")
    public void setLog2X(int log2X){
        LOG2_X = log2X;
    }

    public static int LOG2_Y;
    @Value("${lingjie.Y.log2}")
    public void setLog2Y(int log2Y) {
        LOG2_Y = log2Y;
    }

    public static int LOG2_Z;
    @Value("${lingjie.Z.log2}")
    public void setLog2Z(int log2Z) {
        LOG2_Z = log2Z;
    }

    // X_MASK 是用来计算防止 x 越界的，使用时先 (x & 灵界长度X_MASK)
    public static int 灵界长度X_MASK;

    public static int 灵界宽度Y_MASK;

    public static int 灵界高度Z_MASK;

    public static int 灵界长度X;

    public static int 灵界宽度Y;

    public static int 灵界高度Z;

    public static int 灵界高度Z的一半;

    public static int Y乘Z;

    // 地图文件在磁盘上的位置
    public static String 灵界地图文件;
    @Value("${lingjie.file}")
    public void setLingJieFileName(String str){
        灵界地图文件 = str;
    }

    public static String 灵阵文件;
    @Value("${lingzhen.file}")
    public void setLingZhenFileName(String str) {
        灵阵文件 = str;
    }

    // 没有文件时，创建个随机的文件
    public static Boolean autoCreateFile;
    @Value("${lingjie.file.autoCreate:true}")
    public void setAutoCreateFile(Boolean autoCreateFile) {
        ConfigUtil.autoCreateFile = autoCreateFile;
    }

    // 开启多少个线程
    public static int 线程数;
    @Value("${lingjie.cpu.threadNum:4}")
    public void setThreadNum(int 线程数) {
        ConfigUtil.线程数 = 线程数;
    }

    // 本地缓存，缓存返回给前端的地图碎片
    public static int 缓存最大条数;
    @Value("${lingjie.cache.maxSize:999999}")
    public void setMaxCacheSize(int maxCacheSize) {
        ConfigUtil.缓存最大条数 = maxCacheSize;
    }

    public static int 缓存最大时间;
    @Value("${lingjie.cache.maxSeconds:3600}")
    public void setMaxCacheSeconds(int maxCacheSeconds) {
        ConfigUtil.缓存最大时间 = maxCacheSeconds;
    }

    // 每个线程都会缓存小格子类的对象，这个是最多允许缓存多少个
    public static int maxFreeCellCacheSize;
    @Value("${lingjie.cell.maxFreeCellCacheSize:65536}")
    public void setMaxFreeCellCacheSize(int maxFreeCellCacheSize) {
        ConfigUtil.maxFreeCellCacheSize = maxFreeCellCacheSize;
    }

    public static int 速度多久减少一次;
    @Value("${lingjie.sped.dec.time:8}")
    public void setSpedDescHowTime(int spedDecHowTime){
        ConfigUtil.速度多久减少一次 = spedDecHowTime;
    }

    public static float 水平面引力;
    @Value("${lingjie.gravitation:0.4}")
    public void setGravitation(float gravitation){
        ConfigUtil.水平面引力 = gravitation;
    }

    @PostConstruct
    public void init(){
        // 灵界的长宽高
        灵界长度X = 1 << LOG2_X;
        灵界宽度Y = 1 << LOG2_Y;
        灵界高度Z = 1 << LOG2_Z;
        灵界高度Z的一半 = 灵界高度Z / 2;
        Y乘Z = 灵界宽度Y * 灵界高度Z;
        灵界长度X_MASK = (1<<LOG2_X) - 1;
        灵界宽度Y_MASK = (1<<LOG2_Y) - 1;
        灵界高度Z_MASK = (1<<LOG2_Z) - 1;
    }
}
